<template>
  <div>
    <el-dialog :title="!dataForm.id ? '新增维修单' : '修改维修单'" :close-on-click-modal="false" :visible.sync="visible" center>
      <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
      <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="顾客姓名" prop="customerName">
              <el-input v-model="dataForm.customerName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="customerTel">
              <el-input v-model="dataForm.customerTel"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="维修项目" prop="repairConfigId">
              <el-select v-model="dataForm.repairConfigId" placeholder="请选择">
                <el-option
                  v-for="item in repairConfig"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="维修部门" prop="workDeptId">
              <el-select v-model="dataForm.workDeptId" placeholder="请选择">
                <el-option v-for="item in deptOptions" :key="item.value" :label="item.name" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="维修信息" prop="repairInfo">
          <el-input v-model="dataForm.repairInfo" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dataFormSubmit" :loading="ischeck" v-prevent-re-click>保 存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { remote } from '@/api/admin/dict'
  import { addRepair, checkRepair } from '@/api/eims/repair'

  export default {
    data() {
      return {
        visible: false,
        ischeck: false,
        repairConfig: [],
        deptOptions: [],
        dataForm: {
          id: 0,
          customerName: '',
          customerTel: '',
          repairConfigId: '',
          workDeptId: '',
          repairInfo: ''
        },
        dataRule: {
          customerName: [{required: true, message: '顾客姓名不能为空', trigger: 'blur' }],
          customerTel: [{ required: true, message: '联系电话不能为空', trigger: 'blur' }],
          repairConfigId: [
            { required: true, message: '维修项不能为空', trigger: 'blur' },
          ],
          workDeptId: [{ required: true, message: '请选择维修部门', trigger: 'blur' }]
        }
      }
    },
    computed: {
      ...mapGetters(['bizData'])
    },
    methods: {
      init(id,repairConfig,deptOptions) {
        this.repairConfig = repairConfig
        this.deptOptions = deptOptions
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            checkRepair(this.dataForm.id).then(response => {
							if(response.data.code == 0){
								this.dataForm = response.data.data
							}else{
								this.$message.error(response.data.msg)
							}
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              putAddFeeObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('修改成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                  setTimeout(() => {
                    this.ischeck = false
                  })
                }else{
                  this.$message.error(response.data.msg)
                }
              }).catch((error) => {
                this.ischeck = false
                this.$message.error(error)
              })
            } else {
              addRepair(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('添加成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                  setTimeout(() => {
                    this.ischeck = false
                  })
                }else{
                  this.$message.error(response.data.msg)
                }
              }).catch((err) => {
                this.ischeck = false
                this.$message.error(err)
              })
            }
          }else{
            this.ischeck = false
          }
        })
      }
    }
  }
</script>
